package com.dyz.leetcode;

public class DetectCycle142 {
    //快慢指针
    public ListNode detectCycle(ListNode head) {
        if(head==null || head.next==null){return null;}
        ListNode fast = head;
        ListNode slow = head;
        while (fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast==slow){
                ListNode pr = head;
                while (pr!=slow){
                    pr = pr.next;
                    slow = slow.next;
                }
                return pr;
            }
        }
        return null;
    }
}
